#include "chooselevescenne.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QtDebug>
#include <QTimer>
#include <QLabel>
#include <QSound>

ChooseLeveScenne::ChooseLeveScenne(QWidget *parent) : QMainWindow(parent)
{
    // 配置
    this->setFixedSize(320, 588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("选择关卡");

    // 菜单
    QMenuBar * bar = new QMenuBar();
    setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始");
    QAction  * exitAction = startMenu->addAction("退出");

    // 点击退出，实现退出
    connect(exitAction, &QAction::triggered, [=](){
        this->close();
    });

    // 选择关卡音效
    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav", this);
    // 返回音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);

    // 返回按钮
    MyPushButton * backButoon = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backButoon->setParent(this);
    backButoon->move(this->width()-backButoon->width(), this->height()-backButoon->height());

    // 实现返回按钮功能
    connect(backButoon, &QPushButton::clicked, [=](){
        // 播放返回音效
        backSound->play();

        // 延时返回
        QTimer::singleShot(500, this, [=](){
            emit this->chooseSceneBack();
        });
    });

    // 创建选择关卡的按钮
    for (int i=0; i<20; i++)
    {
        MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+ (i%4) *70, 130 + i/4 * 70);

        // 监听每个按钮的点击事件
        connect(menuBtn, &QPushButton::clicked, [=](){
            // 播放选择关卡音效
            chooseSound->play();

            QString str = QString("您选择的是 %1 关").arg(i+1);
            qDebug() << str;

            // 进入到游戏场景
            this->hide();
            play = new PlayScene(i+1);
            play->show();

            connect(play, &PlayScene::chooseSceneBack, [=](){
                play->hide();
                this->show();
                delete play;
                play = NULL;
            });
        });

        QLabel * lab = new QLabel;
        lab->setParent(this);
        lab->setFixedSize(menuBtn->width(), menuBtn->height());
        lab->setText(QString::number(i+1));
        lab->move(25+ (i%4) *70, 130 + i/4 * 70);
        lab->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        lab->setAttribute(Qt::WA_TransparentForMouseEvents); // 设置鼠标穿透

    }
}

void ChooseLeveScenne::paintEvent(QPaintEvent *)
{
    // 背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0, this->width(), this->height(), pix);

    // 标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5, 30, pix.width(), pix.height(), pix);
}
















